import { Radio, Select, Modal, Checkbox, Col } from 'antd';
import * as React from 'react';
import { ExclamationCircleOutlined } from '@ant-design/icons';

const { confirm } = Modal;


export const buildSelectOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else if (d.name) {
      return <Select.Option key={d.key} value={(d.key).toString()}>{d.name}</Select.Option>
    } else {
      return <Select.Option key={d.key} value={(d.key).toString()}>{d.text}</Select.Option>
    }
  })
}

export const buildIdSelectOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else if (d.name) {
      return <Select.Option key={d.id} value={(d.id).toString()}>{d.name}</Select.Option>
    } else {
      return <Select.Option key={d.id} value={(d.id).toString()}>{d.text}</Select.Option>
    }
  })
}

export const buildIdCheckboxOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else if (d.name) {
    return <Col span={6}><Checkbox key={d.key} value={d.key}>{d.name}</Checkbox></Col>
    } else {
      return <Col span={6}><Checkbox key={d.key} value={(d.key).toString()}>{d.text}</Checkbox> </Col>
    }
  })
}


export const buildNameSelectOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else if (d.name) {
      return <Select.Option key={d.code} value={(d.code).toString()}>{d.name_desc}</Select.Option>
    } else {
      return <Select.Option key={d.key} value={(d.key).toString()}>{d.text}</Select.Option>
    }
  })
}

//排除key为null的
export const buildNoNullSelectOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else if (d.name) {
      return <Select.Option key={d.key} value={(d.key).toString()}>{d.name}</Select.Option>
    } else {
      if(d.key != null){
        return <Select.Option key={d.key} value={(d.key).toString()}>{d.text}</Select.Option>
      }
      
    }
  })
}

export const buildSelectOptionNew = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any,index:any) => {
  if (d.name) {
      return <Select.Option key={index} value={(d.name).toString()}>{d.name}</Select.Option>
    } else {
      return <Select.Option key={index} value={(d.text).toString()}>{d.text}</Select.Option>
    }
  })
}
// 动态属性专用
export const buildSelectOptionAttr = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any,index:any) => {
  if (d.id) {
      return <Select.Option key={index} value={(d.id).toString()}>{d.input_name}</Select.Option>
    } else {
      return <Select.Option key={index} value={(d.id).toString()}>{d.input_name}</Select.Option>
    }
  })
}
// 模板展示图片专用
export const buildSelectOptionTemplate = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any,index:any) => {
  if (d.id) {
      return <Select.Option key={index} value={(d.id).toString()}>{d.text}</Select.Option>
    } else {
      return <Select.Option key={index} value={(d.id).toString()}>{d.text}</Select.Option>
    }
  })
}

//新建标签专用
export const buildSelectOptionTags = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any,index:any) => {
  if (d.id) {
      return <Select.Option key={index} value={(d.id).toString()}>{d.tag_value}</Select.Option>
    } else {
      return <Select.Option key={index} value={(d.id).toString()}>{d.tag_value}</Select.Option>
    }
  })
}

//新建标签专用
export const buildSelectOptionAuthors = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any,index:any) => {
  if (d.id) {
      return <Select.Option key={index} value={(d.id).toString()}>{d.login_name}</Select.Option>
    } else {
      return <Select.Option key={index} value={(d.id).toString()}>{d.login_name}</Select.Option>
    }
  })
}

export const buildRadioOption = (arr: any) => {
  // @ts-ignore
  return arr.map((d: any) => {
    if (d.disabled && d.disabled == true) {
      return undefined;
    } else {
      return <Radio.Button key={d.key} value={d.key}>{d.text}</Radio.Button>
    }
  })
}


export const showDeleteConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认删除选中的数据吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认删除',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showRemoveConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认移除该成员吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认移除',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showCreateConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认新增选中的数据吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认新增',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showSynchronizeConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认选中数据同步到站点吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showReceiveConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认领取活动赠品吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showFreeConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认免单吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}
export const showKeyConfirm = (okType: "default" | "primary" | "ghost" | "dashed" | "danger" | "link", fn: Function, selectedRowKeys: any, callback: Function) => {
  confirm({
    title: '确认重置密钥吗?',
    icon: <ExclamationCircleOutlined />,
    content: '',
    okText: '确认',
    okType: okType,
    cancelText: '取消',
    mask: false,
    getContainer: false, // 注意： 如果不设置这个， 默认是挂载到body上面， 如果页面里面有很多modal弹出， 会出现整体页面无法滚动的情况， 所以挂载在当前节点上面就可以了。
    onOk() {
      return new Promise(async (resolve, reject) => {
        let status = await fn(selectedRowKeys);
        if (status) {
          callback();
          resolve();
        } else {
          reject();
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onCancel() {

    },
  });
}

//根据名称过滤树形结构数据
var searchList:any[] = [];
const searchDepart = (treeData:any,params:any) =>{
  treeData.map((item,index)=>{
    if(item.name == params || item.remark == params){
      searchList=[item]
      console.log(searchList);
    }else{
      if(item.children && item.children.length > 0){
          return searchDepart(item.children,params)
      }
    }
  })
};
export const handleSearchTreeData = (treeData,params) => {
  searchDepart(treeData,params)
  let listData = {
    data:searchList,
  }
  console.log(listData)
  return listData
}
